package org.brisling.ethic.queryService;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.WhereUtil;
import org.brisling.ethic.domain.DicDeptSubject;
import org.brisling.ethic.domain.VODicDeptSubject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/dicDeptSubject/query")
public class QueryDicDeptSubjectService extends AbstractBaseQueryService<DicDeptSubject>{
	
	
	
	
	public QueryDicDeptSubjectService(){
		this.moduleName = ConstSysParam.MODULE_ETHIC_DEPT_SUBJECT;
		this.subSystem = ConstSysParam.SUBSYSTEM_ETHIC;
		this.tableName = this.moduleName;
		this.clazz = DicDeptSubject.class;
		typeMap = this.getTypeMap(clazz);
		
		
		orderMap.put("sortedid","");			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return super.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return super.getBizEditPage();
	}
	
	@RequestMapping("/popPage.action")
	public ModelAndView getPopPage(){
		
		String _page = String.format("/%1$s/%2$s/%3$s", subSystem,moduleName,"popMain");
		return new ModelAndView(_page);
		
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<DicDeptSubject> getListDomain(@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap, true);
		
	}
	
	
	@RequestMapping("/getValidListDomain.action")
	public ListDomain<Object> getValidListDomain(@RequestBody(required=false) String body){		
		
		String sql = " select id,name,item_score,time_score,item_typeid,item_type from " + this.moduleName + " %1$s order by sortedid ";
		return super.getPagedClassListDomain(sql,body, clazz, VODicDeptSubject.class, true);
		
		
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/getInitListDomain.action")
	public ListDomain<Object> getInitListDomain(){		
		
		ListDomain<Object> domain= new ListDomain<Object>();
		String sql = String.format("select * from %1$s order by sortedid", this.tableName);
		List<LinkedHashMap> lists = super.getMappedList(sql);
		if(lists!=null && lists.size()>0){
			List<Object>  objs= super.reflectObjectMaps(DicDeptSubject.class, lists);
			domain.setRows(objs);
			domain.setTotal(new Long( objs.size()));
		}
		return domain;
		
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/getList.action")
	public List<DicDeptSubject> getList(@RequestBody(required=false) String body){		
		String sql = "";
		String whereCorse = null;					
		Map<String,Object> map = this.getBodyParam(body);
		
		Map<String,String> paramMap = (Map<String, String>) map.get(ConstSysParam.CONDITION_MAP_KEY);
		whereCorse = WhereUtil.getConditionWhereCorse(paramMap,typeMap);
		System.out.println("whereCorse: " +whereCorse);		
		
		if(whereCorse!=null && whereCorse.trim().length()>0){			
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
			
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		return super.getAllList(sql);
	}
	
	
	public List<DicDeptSubject> getListAll(Map<String, String> map){	
		
		String sql = "";
		String whereCorse = null;					
			
		whereCorse = WhereUtil.getConditionWhereCorse(map,typeMap);
		System.out.println("whereCorse: " +whereCorse);		
		
		if(whereCorse!=null && whereCorse.trim().length()>0){			
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
			
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		return super.getAllList(sql);
		
	}
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public DicDeptSubject getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public DicDeptSubject getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}	
	
	
	@RequestMapping("/getListByField.action")
	public List<DicDeptSubject> getListByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getListByIdentify(field,fieldVal);
	}
}
